package com.sudo.common.validenum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author 为道日损 2020-06-17 21:12
 * webSite：https://www.sudo.ren
 */
public class EnumValueValidator implements ConstraintValidator<EnumValue, Object> {

    private String[] strValues;
    private int[] intValues;

    @Override
    public void initialize(EnumValue constraintAnnotation) {
        strValues = constraintAnnotation.strValues();
        intValues = constraintAnnotation.intValues();
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext context) {
        if (o instanceof String) {
            for (String s : strValues) {
                if (s.equals(o)) {
                    return true;
                }
            }
        } else if (o instanceof Integer) {
            for (Integer i : intValues) {
                if (i == o) {
                    return true;
                }
            }
        }
        return false;
    }
}
